/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package GestorBDatos;

import Hash.PasswordHasher;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.*;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author julian
 */
@Entity
@Table(name = "usuario")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Usuario.findAll", query = "SELECT u FROM Usuario u"),
    @NamedQuery(name = "Usuario.findByLogin", query = "SELECT u FROM Usuario u WHERE u.login = :login"),
    @NamedQuery(name = "Usuario.findByNombre", query = "SELECT u FROM Usuario u WHERE u.nombre = :nombre"),
    @NamedQuery(name = "Usuario.findByApellidos", query = "SELECT u FROM Usuario u WHERE u.apellidos = :apellidos"),
    @NamedQuery(name = "Usuario.findByCorreo", query = "SELECT u FROM Usuario u WHERE u.correo = :correo"),
    @NamedQuery(name = "Usuario.findByIp", query = "SELECT u FROM Usuario u WHERE u.ip = :ip"),
    @NamedQuery(name = "Usuario.findByTipo", query = "SELECT u FROM Usuario u WHERE u.tipo = :tipo"),
    @NamedQuery(name = "Usuario.findByIntentosAutentificacion", query = "SELECT u FROM Usuario u WHERE u.intentosAutentificacion = :intentosAutentificacion"),
    @NamedQuery(name = "Usuario.findByHoraPrimerIntento", query = "SELECT u FROM Usuario u WHERE u.horaPrimerIntento = :horaPrimerIntento"),
    @NamedQuery(name = "Usuario.findByDadoAltaTipo", query = "SELECT u FROM Usuario u WHERE u.dadoAlta = :dadoAlta and u.tipo = :tipo"),
    @NamedQuery(name = "Usuario.findByDadoAlta", query = "SELECT u FROM Usuario u WHERE u.dadoAlta = :dadoAlta")})
public class Usuario implements Serializable {
    @Basic(optional = false)
    @Lob
    @Column(name = "hashPass")
    private byte[] hashPass;
    @Basic(optional = false)
    @Lob
    @Column(name = "salt")
    private byte[] salt;
    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @Column(name = "login")
    private String login;
    @Basic(optional = false)
    @Column(name = "nombre")
    private String nombre;
    @Basic(optional = false)
    @Column(name = "apellidos")
    private String apellidos;
    @Basic(optional = false)
    @Column(name = "correo")
    private String correo;
    @Basic(optional = false)
    @Column(name = "ip")
    private String ip;
    @Basic(optional = false)
    @Column(name = "tipo")
    private String tipo;
    @Column(name = "intentosAutentificacion")
    private int intentosAutentificacion;
    @Column(name = "horaPrimerIntento")
    @Temporal(TemporalType.TIMESTAMP)
    private Date horaPrimerIntento;
    @Basic(optional = false)
    @Column(name = "dadoAlta")
    private String dadoAlta;
    @Basic(optional = false)
    @Column(name = "fechaPeticionAlta")
    @Temporal(TemporalType.TIMESTAMP)
    private Date fechaPeticionAlta;
    @OneToOne(cascade = CascadeType.ALL, mappedBy = "usuario")
    private Reset reset;

    public Usuario() {
    }

    public Usuario(String login) {
        this.login = login;
    }

    public Usuario(String login, String nombre, String apellidos, String correo, String pass, String ip, String tipo, String dadoAlta, Date fechaPeticionAlta) {
        this.login = login;
        this.nombre = nombre;
        this.apellidos = apellidos;
        this.correo = correo;
        this.ip = ip;
        this.tipo = tipo;
        this.dadoAlta = dadoAlta;
        this.fechaPeticionAlta = fechaPeticionAlta;
        
        PasswordHasher.PasswordHash ph = PasswordHasher.hash(pass);
        this.hashPass = ph.getHash();
        this.salt = ph.getSalt();     
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getApellidos() {
        return apellidos;
    }

    public void setApellidos(String apellidos) {
        this.apellidos = apellidos;
    }

    public String getCorreo() {
        return correo;
    }

    public void setCorreo(String correo) {
        this.correo = correo;
    }

    public void setPass(String pass) {
        PasswordHasher.PasswordHash ph = PasswordHasher.hash(pass);
        this.hashPass = ph.getHash();
        this.salt = ph.getSalt();
    }

    public String getIp() {
        return ip;
    }

    public void setIp(String ip) {
        this.ip = ip;
    }

    public String getTipo() {
        return tipo;
    }

    public void setTipo(String tipo) {
        this.tipo = tipo;
    }

    public int getIntentosAutentificacion() {
        return intentosAutentificacion;
    }

    public void setIntentosAutentificacion(int intentosAutentificacion) {
        this.intentosAutentificacion = intentosAutentificacion;
    }

    public Date getHoraPrimerIntento() {
        return horaPrimerIntento;
    }
    
    public Date getfechaPeticionAlta() {
        return this.fechaPeticionAlta;
    }

    public void setHoraPrimerIntento(Date horaPrimerIntento) {
        this.horaPrimerIntento = horaPrimerIntento;
    }

    public String getDadoAlta() {
        return dadoAlta;
    }

    public void setDadoAlta(String dadoAlta) {
        this.dadoAlta = dadoAlta;
    }

    public Reset getReset() {
        return reset;
    }

    public void setReset(Reset reset) {
        this.reset = reset;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (login != null ? login.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Usuario)) {
            return false;
        }
        Usuario other = (Usuario) object;
        if ((this.login == null && other.login != null) || (this.login != null && !this.login.equals(other.login))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "GestorBd.Usuario[ login=" + login + " ]";
    }

    public byte[] getHashPass() {
        return hashPass;
    }

    public void setHashPass(byte[] hashPass) {
        this.hashPass = hashPass;
    }

    public byte[] getSalt() {
        return salt;
    }

    public void setSalt(byte[] salt) {
        this.salt = salt;
    }
    
}
